package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.GatewaySearchDto;
import com.example.front.entity.Level;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LevelMapper {

    @Insert("INSERT INTO `level`" +
            " (`tenant_id`,`name`,`duration`,`times`,`pid`) VALUES" +
            " (#{tenantId}, #{name}, #{duration},#{times},#{pid})")
    void create(Level level);
//    LIMIT #{pagination.offset}, #{pagination.limit}
    @Select("<script>" +
            "SELECT * FROM `level` " +
            "<where>" +
            " `tenant_id`=#{level.tenantId} " +
            "<if test='level.id!=0 or level.id=null'> AND `id`!=#{level.id}</if>" +
            "<if test='level.name!=null'> AND `name`=#{level.name}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `duration`" +
            "</script>")
    List<Level> findAll(@Param("level") Level level, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `level` " +
            "<where>" +
            " `tenant_id`=#{level.tenantId} " +
            "<if test='level.id!=0'> AND `id`!=#{level.id}</if>" +
            "<if test='level.name!=null'> AND `name`=#{level.name}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE+
            "</script>")
    long count(@Param("level") Level level);

    @Select("<script>" +
            "SELECT * FROM `level` " +
            "where id=#{id}" +
            " AND `status`!=" + User.STATUS_DELETE+
            "</script>")
    Level findById(Long id);

    @Select("<script>" +
            "SELECT * FROM `level` " +
            "where pid=#{id}" +
            "</script>")
    Level findByPid(Long id);

    @Update("UPDATE `level` SET `name` = #{name} , `duration` = #{duration},`pid`=#{pid},`times`=#{times},`handle`=#{handle}"+
            " WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    int update(Level level);

    @Update("UPDATE `level` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(Level level);
}
